package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.EcologyPoolCommunityServiceRankConfDao;
import com.sc.nft.dao.EcologyPoolCommunityServiceRankDao;
import com.sc.nft.dao.EcologyPoolCommunityServiceRankIncomeConditionConfDao;
import com.sc.nft.dao.EcologyPoolCommunityServiceRankIncomeConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.enums.EcologyPoolCommunityServiceRankTypeEnum;
import com.sc.nft.service.EcologyPoolCommunityServiceRankService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyPoolCommunityServiceRankServiceImpl implements EcologyPoolCommunityServiceRankService {

    private final EcologyPoolCommunityServiceRankDao ecologyPoolCommunityServiceRankDao;
    private final EcologyPoolCommunityServiceRankConfDao ecologyPoolCommunityServiceRankConfDao;
    private final EcologyPoolCommunityServiceRankIncomeConfDao ecologyPoolCommunityServiceRankIncomeConfDao;
    private final EcologyPoolCommunityServiceRankIncomeConditionConfDao ecologyPoolCommunityServiceRankIncomeConditionConfDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchExchequer(List<EcologyPoolCommunityServiceRank> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        ecologyPoolCommunityServiceRankDao.saveBatchExchequer(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rankProcessing(EcologyPoolCommunityServiceRankConf ecologyPoolCommunityServiceRankConf) {
        // 获取当前符合的排行榜活动,把排行榜活动拿出来以后 循环处理
        List<EcologyPoolCommunityServiceRankIncomeConf> configId = ecologyPoolCommunityServiceRankIncomeConfDao.getConfigId(ecologyPoolCommunityServiceRankConf.getId());
        configId.forEach(ecologyPoolCommunityServiceRankIncomeConf -> {
            List<Long> userIdExcludeList = new ArrayList<>();
            EcologyPoolCommunityServiceRankIncomeConditionConf rankConfIdAndIncomeConfigId = ecologyPoolCommunityServiceRankIncomeConditionConfDao.getRankConfIdAndIncomeConfigId(ecologyPoolCommunityServiceRankConf.getId(), ecologyPoolCommunityServiceRankIncomeConf.getId());
            for (int i = 0; i < ecologyPoolCommunityServiceRankIncomeConf.getEndRank() - ecologyPoolCommunityServiceRankIncomeConf.getStartRank(); i++) {
                BigDecimal newEcologySmeltingThreshold = ecologyPoolCommunityServiceRankConf.getNewEcologySmeltingThreshold() != null ? ecologyPoolCommunityServiceRankConf.getNewEcologySmeltingThreshold() : BigDecimal.ZERO;
                Integer newUserCountThreshold = ecologyPoolCommunityServiceRankConf.getNewUserCountThreshold() != null ? ecologyPoolCommunityServiceRankConf.getNewUserCountThreshold() : 0;
                EcologyPoolCommunityServiceRank ecologyPoolCommunityServiceRank = null;
                if (BeanUtil.isNotEmpty(rankConfIdAndIncomeConfigId)) {
                    ecologyPoolCommunityServiceRank = ecologyPoolCommunityServiceRankDao.listByRankConfIdAndcContributionThreshold(ecologyPoolCommunityServiceRankConf.getId(), newEcologySmeltingThreshold, newUserCountThreshold, rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold() != null ? rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold() : BigDecimal.ZERO, rankConfIdAndIncomeConfigId.getNewUserCountThreshold() != null ? rankConfIdAndIncomeConfigId.getNewUserCountThreshold() : 0, userIdExcludeList);
                } else {
                    ecologyPoolCommunityServiceRank = ecologyPoolCommunityServiceRankDao.listByRankConfIdAndcContributionThreshold(ecologyPoolCommunityServiceRankConf.getId(), newEcologySmeltingThreshold, newUserCountThreshold, BigDecimal.ZERO, 0, userIdExcludeList);
                }
                if (BeanUtil.isNotEmpty(ecologyPoolCommunityServiceRank)) {
                    EcologyPoolCommunityServiceRank result = new EcologyPoolCommunityServiceRank();
                    result.setId(ecologyPoolCommunityServiceRank.getId());
                    result.setSort(ecologyPoolCommunityServiceRankIncomeConf.getStartRank() + i + 1L);
                    EcologyPoolCommunityServiceRank communityServiceRank = ecologyPoolCommunityServiceRankDao.compareDaoRank(ecologyPoolCommunityServiceRank.getUserId(), DateUtil.offsetDay(DateUtil.date(), -1), ecologyPoolCommunityServiceRank.getRankType());
                    if (BeanUtil.isNotEmpty(communityServiceRank)) {
                        result.setSortChange(Convert.toInt(result.getSort() - communityServiceRank.getSort()));
                    } else {
                        result.setSortChange(-1);
                    }
                    result.setHasSort(Boolean.TRUE);
                    result.setShowStatus(Boolean.TRUE);
                    result.setPrizeRatio(ecologyPoolCommunityServiceRankIncomeConf.getPrizeRatio());
                    result.updateById();
                    userIdExcludeList.add(ecologyPoolCommunityServiceRank.getUserId());
                } else {
                    EcologyPoolCommunityServiceRank result = new EcologyPoolCommunityServiceRank();
                    result.setEcologyPoolCommunityServiceRankConfId(ecologyPoolCommunityServiceRankConf.getId());
                    result.setRankDate(DateUtil.date());
                    result.setDaoId(0L);
                    result.setNewEcologySmelting(BigDecimal.ZERO);
                    result.setSort(ecologyPoolCommunityServiceRankIncomeConf.getStartRank() + i + 1L);
                    if (ecologyPoolCommunityServiceRankConf.getRankType() == EcologyPoolCommunityServiceRankTypeEnum.SERVICE_CENTER) {
                        if (BeanUtil.isEmpty(rankConfIdAndIncomeConfigId) || rankConfIdAndIncomeConfigId.getNewUserCountThreshold() == null || rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold() == null) {
                            result.setRankName(StrUtil.format("暂无入榜"));
                        } else {
                            result.setRankName(StrUtil.format("上榜条件：新增网点数>={},新增熔炼值>={}", rankConfIdAndIncomeConfigId.getNewUserCountThreshold(), rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold().setScale(2,BigDecimal.ROUND_DOWN)));
                        }

                    } else {
                        if (BeanUtil.isEmpty(rankConfIdAndIncomeConfigId) || rankConfIdAndIncomeConfigId.getNewUserCountThreshold() == null || rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold() == null) {
                            result.setRankName(StrUtil.format("暂无入榜"));
                        } else {
                            result.setRankName(StrUtil.format("上榜条件：新增用户数>={},新增熔炼值>={}", rankConfIdAndIncomeConfigId.getNewUserCountThreshold(), rankConfIdAndIncomeConfigId.getNewEcologySmeltingThreshold().setScale(2,BigDecimal.ROUND_DOWN)));
                        }
                    }
                    result.setHasSort(Boolean.FALSE);
                    result.setSortChange(-1);
                    result.setShowStatus(Boolean.TRUE);
                    result.setNewUserCount(0);
                    result.setUserId(0L);
                    result.setRankType(ecologyPoolCommunityServiceRankConf.getRankType());
                    result.insert();
                }
            }
        });
    }
}
